Imports System
Imports System.Collections.Generic
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports OMANE.Common.UI
Imports ICSharpCode.AvalonEdit.Highlighting
Imports System.CodeDom.Compiler
Imports System.Linq.Expressions
Imports System.Data
Imports OMANE.Modules.Builder.MSFunctions
Imports OMANE.Modules.Builder.MSExpression
Imports ICSharpCode.AvalonEdit.CodeCompletion
Imports System.ComponentModel
Imports System.Data.Common


Public Class MSTable
    Inherits MSObject

    Dim _Properties As IEnumerable(Of MSProperty)
    Dim _Universe As MSUniverse

    Public Property Name As String
    Public Property IsTemporary As Boolean = False
    Public Property Param As MSParam

    Sub New(ByVal universe As MSUniverse)
        Me._Universe = universe
    End Sub

    Public Function GetCreateSql() As String

    End Function

    <Runtime.CompilerServices.Dynamic()> _
    Public ReadOnly Property Queries(Optional ByVal Name As String = "_unnamed_query_") As Object
        Get
            Return New MSOperation(Me._Universe)
        End Get
    End Property

    Public Property Properties As IEnumerable(Of MSProperty)
        Get
            Return Me._Properties
        End Get
        Protected Set(ByVal value As IEnumerable(Of MSProperty))
            Me._Properties = value
        End Set
    End Property
End Class

Public Class MSParam
    Inherits MSExpression

    Dim _Properties As New List(Of MSProperty)

    Property Locked As Boolean = False

    Public ReadOnly Property Properties As IEnumerable(Of MSProperty)
        Get
            Return Me._Properties
        End Get
    End Property

    Sub Update()

    End Sub
    Property Table As MSTable

    Sub AddProperty(ByVal prop As MSProperty)
        If Not Locked Then
            If Not _Properties.Contains(prop) Then
                _Properties.Add(prop)
            End If

            If _Properties.Count > 1 Then

            End If
        End If
    End Sub
    Sub AddProperty(ByVal prop As String)

    End Sub
    Sub RemoveProperty(ByVal prop As MSProperty)
        If Not Locked Then
            _Properties.Remove(prop)
        End If
    End Sub
    Sub RemoveProperty(ByVal prop As String)

    End Sub
End Class
Public Class MSParam(Of T)
    Inherits MSParam

    Private maintable As MSTable

    Sub New(ByVal name As String, ByVal universe As MSUniverse)
        Table = New MSTable(universe)
        Table.IsTemporary = True
        Table.Name = "param_table_" & name
        Table.Param = Me
        Me.maintable = universe.MainTable
        Me.Name = name
        Me.FuncStr = "{0}.{1}"
        Sources = New MSTable() {Me.Table}
        Me.Parameters = New Func(Of String)() { _
            Function() Table.Name, _
            Function() Me.Name}

    End Sub

    Property Name As String
    Property IsConstant As Boolean = True
    Property ValueProperty As MSProperty(Of T)
    Property AutoCreateTable As Boolean
End Class

Public Class MSUniverse


    Public Property Connection As IDbConnection

    Public Property MainTable As MSTable
    Public Property Params As IEnumerable(Of MSParam)
End Class


